﻿
using System;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnterpriseServices.Service
{
    public partial class ValidationManagerJSONProxy : IDisposable, IValidationManagerJSON
    {

        /// <summary>
        /// Provides the singleton pattern.
        /// </summary>
        /// <returns></returns>
        public static ValidationManagerJSONProxy sharedInstance()
        {
            return SHARED_INSTANCE;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public void Initialize()
        {
            if (!initialized)
            {
                timeout.AutoReset = true;
                timeout.Elapsed += new System.Timers.ElapsedEventHandler(channelManager.timeout_Elapsed);
                timeout.Enabled = true;
                initialized = true;
            }
        }


        #region IValidationManagerJSON Members

        public bool DoesUserExist(string Username)
        {
            Initialize();
            bool doesUserExist = channelManager.ChannelSetup(ChannelTask.Open).DoesUserExist(Username);
            return doesUserExist;
        }

        public bool DoesUserExistPass(string Username, string Password)
        {
            Initialize();
            bool doesUserExistPass = channelManager.ChannelSetup(ChannelTask.Open).DoesUserExistPass(Username, Password);
            return doesUserExistPass;
        }

        public bool IsUserInRole(string ApplicationName, string RoleName, string UserName)
        {
            Initialize();
            bool isUserInRole = channelManager.ChannelSetup(ChannelTask.Open).IsUserInRole(ApplicationName, RoleName, UserName);
            return isUserInRole;
        }

        public bool IsUserInRolePass(string ApplicationName, string RoleName, string UserName, string Password)
        {
            Initialize();
            bool isUserInRolePass = channelManager.ChannelSetup(ChannelTask.Open).IsUserInRolePass(ApplicationName, RoleName, UserName, Password);
            return isUserInRolePass;
        }

        public string[] GetUsersByApplicationName(string ApplicationName)
        {
            Initialize();
            string[] getUsersByApplicationName = channelManager.ChannelSetup(ChannelTask.Open).GetUsersByApplicationName(ApplicationName);
            return getUsersByApplicationName.ToArray();
        }

        public bool IsGroupInRole(string ApplicationName, string RoleName, string GroupName)
        {
            Initialize();
            bool isGroupInRole = channelManager.ChannelSetup(ChannelTask.Open).IsGroupInRole(ApplicationName, RoleName, GroupName);
            return isGroupInRole;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            timeout.Enabled = false;
            channelManager.ChannelSetup(ChannelTask.Close);
        }

        #endregion




        #region Variables declaration

        // this private field is need to support the singleton pattern
        private static ValidationManagerJSONProxy SHARED_INSTANCE = new ValidationManagerJSONProxy();

        bool initialized = false;
        const string endpointName = "IValidationManagerJSON.Endpoint";
        ChannelManager<IValidationManagerJSON> channelManager = new ChannelManager<IValidationManagerJSON>(endpointName);
        System.Timers.Timer timeout = new System.Timers.Timer(60000); // this should not below 1000, else there is no benefit

        #endregion


    }
}
